﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainLeaveApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private HrLeaveApplicationEntity mSelectedLeaveApp;
        public ApproveType ThisApproveType { get; set; }
        #endregion

        #region Constructor
        public frmMaintainLeaveApplication(HrLeaveApplicationEntity entity)
        {
            InitializeComponent();
            mSelectedLeaveApp = entity;
        }

        public frmMaintainLeaveApplication(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }

        public frmMaintainLeaveApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtDepartment.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(mSelectedPerson.DepartmentId);
            if (department != null)
            {
                txtDepartment.Text = department.DepartmentName;
            }
            MstPositionEntity position = MstPositionBs.GetMstPositionByID(mSelectedPerson.PositionId);
            if (position != null)
            {
                txtPosition.Text = position.PositionName;
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "LAS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbLeaveType, "LPTYPE", false);
            cmbStatus.SelectedIndex = 0;
            cmbLeaveType.SelectedIndex = 0;
        }

        private void SetFields()
        {
            if (mSelectedLeaveApp == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedLeaveApp.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedLeaveApp.PersonId);
                SetPersonFields();
            }
            if (mSelectedLeaveApp.ApplicationDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedLeaveApp.ApplicationDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpApplicationDate.EditValue = mSelectedLeaveApp.ApplicationDate;
            }
            if (mSelectedLeaveApp.StartDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedLeaveApp.StartDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpStartDate.EditValue = mSelectedLeaveApp.StartDate;
            }
            if (mSelectedLeaveApp.EndDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedLeaveApp.EndDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpEndDate.EditValue = mSelectedLeaveApp.EndDate;
            }
            cmbStatus.SetSelectedValue(mSelectedLeaveApp.Status);
            cmbLeaveType.SetSelectedValue(mSelectedLeaveApp.LeaveType);
            txtDayCount.Text = mSelectedLeaveApp.DayCount.ToString("#0.00");
            txtLeaveReason.Text = mSelectedLeaveApp.LeaveReason;

            txtDmRemark.Text = mSelectedLeaveApp.DmRemark;
            UICommonFunctions.SetTextBoxDate(txtDmApprovedDate, mSelectedLeaveApp.DmApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtDmPerson, mSelectedLeaveApp.DmPerson);
            txtCeoRemark.Text = mSelectedLeaveApp.CeoRemark;
            UICommonFunctions.SetTextBoxDate(txtCeoApprovedDate, mSelectedLeaveApp.CeoApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCeoPerson, mSelectedLeaveApp.CeoPerson);

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedLeaveApp.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedLeaveApp.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedLeaveApp.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedLeaveApp.LastModifiedBy);
        }

        private void SetEntity()
        {
            if (mSelectedLeaveApp == null)
            {
                mSelectedLeaveApp = new HrLeaveApplicationEntity();
                mSelectedLeaveApp.ApplicationId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedLeaveApp.PersonId = mSelectedPerson.PersonId;
                mSelectedLeaveApp.DepartmentId = mSelectedPerson.DepartmentId;
                mSelectedLeaveApp.PositioinId = mSelectedPerson.PositionId;
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                mSelectedLeaveApp.ApplicationDate = Convert.ToDateTime(dtpApplicationDate.EditValue);
            }
            else
            {
                mSelectedLeaveApp.ApplicationDate = Convert.ToDateTime("1900-01-01");
            }
            if (dtpStartDate.EditValue!=null)
            {
                mSelectedLeaveApp.StartDate = Convert.ToDateTime(dtpStartDate.EditValue);
            }
            else
            {
                mSelectedLeaveApp.StartDate = Convert.ToDateTime("1900-01-01 00:00");
            }
            if (dtpEndDate.EditValue!=null)
            {
                mSelectedLeaveApp.EndDate = Convert.ToDateTime(dtpEndDate.EditValue);
            }
            else
            {
                mSelectedLeaveApp.EndDate = Convert.ToDateTime("1900-01-01 00:00");
            }
            if (cmbLeaveType.SelectedValue != null)
            {
                mSelectedLeaveApp.LeaveType = cmbLeaveType.SelectedValue.ToString();
            }
            mSelectedLeaveApp.Status = "LAS01";
            mSelectedLeaveApp.DayCount = CommonFunctions.ToDecimal(txtDayCount.Text);
            mSelectedLeaveApp.LeaveReason = txtLeaveReason.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedLeaveApp.CreatedDate = DateTime.Now;
                mSelectedLeaveApp.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedLeaveApp.LastModifiedDate = DateTime.Now;
                mSelectedLeaveApp.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(cmbLeaveType.Text))
            {
                MessageBox.Show("请假类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtDayCount.Text) || CommonFunctions.ToDecimal(txtDayCount.Text) == 0)
            {
                MessageBox.Show("请假天数不能为空或者0！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtLeaveReason.Text))
            {
                MessageBox.Show("请假事由不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpApplicationDate.EditValue==null)
            {
                MessageBox.Show("申请日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpStartDate.EditValue == null )
            {
                MessageBox.Show("请假开始日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpEndDate.EditValue == null )
            {

                MessageBox.Show("请假结束日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (dtpStartDate.EditValue != null || txtDayCount.Text != null)
            {
                DateTime D1 = Convert.ToDateTime(dtpStartDate.EditValue.ToString());
                string day = txtDayCount.Text;
                double days = double.Parse(day);

                dtpEndDate.EditValue = D1.AddDays(days - 1.0);
                return true;

            }
            if (dtpEndDate.EditValue != null || txtDayCount.EditValue != null)
            {
                DateTime D2 = Convert.ToDateTime(dtpEndDate.EditValue.ToString());
                string day = txtDayCount.Text;
                int days = int.Parse(day);
                dtpStartDate.EditValue = D2.AddDays(days);
                return true;
            }
            if (dtpStartDate.EditValue != null || dtpEndDate.EditValue != null)
            {
                DateTime D1 = Convert.ToDateTime(dtpStartDate.EditValue.ToString());
                DateTime D2 = Convert.ToDateTime(dtpEndDate.EditValue.ToString()).AddDays(1);
                TimeSpan span = D2.Subtract(D1);
                txtDayCount.Text = span.Days.ToString();
                return true;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = HrLeaveApplicationBs.InsertHrLeaveApplication(mSelectedLeaveApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = HrLeaveApplicationBs.UpdateHrLeaveApplication(mSelectedLeaveApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    switch (ThisApproveType)
                    {
                        case ApproveType.DM:
                            mSelectedLeaveApp.DmApprovedDate = DateTime.Now;
                            mSelectedLeaveApp.DmPerson = CommonParameter.CurrentUser.PersonId;
                            mSelectedLeaveApp.DmRemark = txtDmRemark.Text;
                            break;
                        case ApproveType.CEO:
                            mSelectedLeaveApp.CeoApprovedDate = DateTime.Now;
                            mSelectedLeaveApp.CeoPerson = CommonParameter.CurrentUser.PersonId;
                            mSelectedLeaveApp.CeoRemark = txtCeoRemark.Text;
                            break;
                        default:
                            return false;
                    }
                    bool isSuccessful = HrLeaveApplicationBs.UpdateHrLeaveApplication(mSelectedLeaveApp);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ResetControlsEnable()
        {
            txtDmRemark.Properties.ReadOnly = true;
            txtCeoRemark.Properties.ReadOnly = true;
            if (ThisActionType == ActionType.Approve)
            {
                switch (ThisApproveType)
                {
                    case ApproveType.DM:
                        txtDmRemark.Properties.ReadOnly = false;
                        break;
                    case ApproveType.CEO:
                        txtCeoRemark.Properties.ReadOnly = false;
                        break;
                }
            }
        }
        #endregion

        #region Event
        private void frmMaintainLeaveApplication_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetPersonFields();
            SetFields();
            ResetControlsEnable();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedLeaveApp == null)
            {
                return;
            }
            switch (ThisApproveType)
            {
                case ApproveType.DM:
                    mSelectedLeaveApp.Status = "LAS02";
                    break;
                case ApproveType.CEO:
                    mSelectedLeaveApp.Status = "LAS04";
                    break;
                default:
                    return;
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            if (mSelectedLeaveApp == null)
            {
                return;
            }
            switch (ThisApproveType)
            {
                case ApproveType.DM:
                    mSelectedLeaveApp.Status = "LAS03";
                    break;
                case ApproveType.CEO:
                    mSelectedLeaveApp.Status = "LAS05";
                    break;
                default:
                    return;
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }

        private void txtDayCount_EditValueChanged(object sender, EventArgs e)
        {
            //DateTime D1 = Convert.ToDateTime(dtpStartDate .EditValue .ToString ());
            //DateTime D2 = Convert.ToDateTime(dtpEndDate.EditValue.ToString());
            //TimeSpan span = D2.Subtract(D1);
            //txtDayCount.Text = span.Days.ToString();
        }

        #endregion
    }
}
